package com.codename1.samples;


import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.testing.AbstractTest;
import com.codename1.ui.Button;
import com.codename1.ui.Component;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ArrayTests2768 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        Button b = new Button("Run Test");
        SpanLabel result = new SpanLabel();
        b.addActionListener(e->{
            try {
                if (new MyTest().runTest()) {
                    result.setText("Success");
                } else {
                    result.setText("Failed");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                result.setText("Failed: "+ex.getMessage());
            }
            hi.revalidateWithAnimationSafety();
        });
        hi.addAll(b, result);
        hi.show();
    }
    
    private class MyTest extends AbstractTest {

        @Override
        public boolean runTest() throws Exception {
            
            Label[] labelArr = new Label[10];
            Component[] componentArr = new Component[5];
            Object[] objArr = new Object[4];
            Class[] classArr = new Class[3];
            
            assertEqual(Label[].class, labelArr.getClass());
            assertEqual(Component[].class, componentArr.getClass());
            assertEqual(Object[].class, objArr.getClass());
            assertEqual(Class[].class, classArr.getClass());
            
            /*
            // Uncomment this section to test Array.newInstance() in ParparVM
            // You also need to disable compliance checking for CLDC11
            // in the SampleApplicationTemplate's build.xml file
            // since CLDC doesn't have Array class
            // This tests issue reported in 2768
            // This is relevant only for Kotlin since Kotlin slips these 
            // non-CLDC APIs in under the covers.
            Object[] objArr2 = (Object[])java.lang.reflect.Array.newInstance(Object.class, 1);
            assertEqual(Object[].class, objArr2.getClass());
            
            Class[] classArr2 = (Class[])java.lang.reflect.Array.newInstance(Class.class, 1);
            assertEqual(Class[].class, classArr2.getClass());
            */
            
            return true;
            
        }
        
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
